package com.baosight.risk.service.fltd.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.baosight.iplat4j.core.util.DateUtils;
import com.baosight.risk.api.fltd.FltdOATaskService;
import com.baosight.risk.api.xtaa.code.IXtaaCodeService;
import com.baosight.risk.common.bean.BeanUtils;
import com.baosight.risk.common.utils.HttpClientUtil;
import com.baosight.risk.mapper.fltd.FltdNoticeMapper;
import com.baosight.risk.service.fltd.entity.FltdNotice;
import com.baosight.risk.service.fltd.entity.FltdTask;
import com.baosight.risk.service.xtaa.code.entity.XtaaCode;
import com.google.common.collect.Maps;

import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONObject;
@Service("fltdOATaskServiceImpl")
@Slf4j
public class FltdOATaskServiceImpl implements FltdOATaskService {
    @Autowired
    private IXtaaCodeService xtaaCodeService;
    @Autowired
    private FltdNoticeMapper fltdNoticeMapper;
    
	@Override
	public void insertOA(FltdTask task) throws Exception {
		this.deleteOA(task);
		
		this.insertNotice(task);
		
		Map<String,Object> map = Maps.newHashMap();
		map.put("syscode","risk");
		map.put("flowid","risk-"+task.getFlowNo()+"-V1-"+task.getTodoGuid());
		map.put("requestname",task.getBizDesc());
		map.put("workflowname",task.getFlowName());
		map.put("nodename",task.getNodeName());
		map.put("pcurl",getUrlByDdCode("C"));
		map.put("isremark","0");//待办
		map.put("viewtype","0");//待办
		map.put("creator",task.getCurrUserCode());
		map.put("createdatetime",DateUtils.toDateStr(task.getCreateTime(),DateUtils.DEFAULT_DATETIME_PATTERN));
		map.put("receiver",task.getCurrUserCode());
		map.put("receivedatetime",DateUtils.toDateStr(task.getFactStartDate(),DateUtils.DEFAULT_DATETIME_PATTERN));
		map.put("receivets",String.valueOf(System.currentTimeMillis()));
		map.put("signver","V0");
		//JSONObject jsonObject = JSONObject.fromObject(map);
		String jsonData = JSON.toJSONString(map);

		String result = HttpClientUtil.doPostJson(getUrlByDdCode("A"),jsonData);
		JSONObject js = JSONObject.fromObject(result);
		if (!"1".equals(js.getString("operResult"))) {
			System.out.println("oa4.0 insert error:"+js.getString("message"));
		}
	}

	@Override
	public void deleteOA(FltdTask task) throws Exception {
		// TODO Auto-generated method stub
		String flowid = "risk-"+task.getFlowNo()+"-V1-"+task.getTodoGuid();
		Map<String,Object> map = Maps.newHashMap();
		map.put("syscode","risk");
		map.put("flowid",flowid);
		map.put("userid",task.getCurrUserCode());
		//JSONObject jsonObject = JSONObject.fromObject(map);
		String jsonData = JSON.toJSONString(map);

		String result = HttpClientUtil.doPostJson(getUrlByDdCode("B"),jsonData);
		JSONObject js = JSONObject.fromObject(result);
		if (!"1".equals(js.getString("operResult"))) {
			System.out.println("oa4.0 delete error:"+js.getString("message"));
		}else{
			FltdNotice param = new FltdNotice();
			param.setCurrUserCode(task.getCurrUserCode());
			param.setFlowId(flowid);
			param.setStatus("active");
			List<FltdNotice> activeNotices = fltdNoticeMapper.select(param);
			for(FltdNotice notice : activeNotices){
				notice.setStatus("close");
				fltdNoticeMapper.updateByPrimaryKeySelective(notice);
			}
		}
	}
	
    private String getUrlByDdCode(String ddCode){
    	List<XtaaCode> xtaaCodes = xtaaCodeService.selectDataByDDTypeCode("OA_URL");
    	for(XtaaCode xtaaCode : xtaaCodes){
    		if(ddCode.equals(xtaaCode.getDdCode())){
    			return xtaaCode.getDdDesc();
    		}
    	}
    	return null;
    }
    
    private void insertNotice(FltdTask task){
        FltdNotice notice = new FltdNotice();
        BeanUtils.initBean(notice);
        notice.setFlowId("risk-"+task.getFlowNo()+"-V1-"+task.getTodoGuid());
        notice.setCurrUserCode(task.getCurrUserCode());
        notice.setCurrUserName(task.getCurrUserName());
        notice.setStatus("active");
        notice.setFrom("admin");
        notice.setTo(task.getCurrUserCode());
        notice.setUpdateBy(null);
        notice.setUpdateTime(null);
        fltdNoticeMapper.insert(notice);
    }

}
